package com.mall.implementation;

import java.util.ArrayList;
import java.util.Scanner;

import com.mall.implementation.CustomerImpl;
import com.mall.util.StringWrapper;
import com.mall.implementation.StoreImpl;
import com.mall.implementation.StoreOwnerImpl;
import com.mall.core.Visitor;
import com.mall.core.service.auth.Authenticator;

public class MallApplication {

	public static void assignShoppingCarts(Visitor visitor) {

	}

	public static void retrieveShopingCart(Visitor visitor) {
	}

	public static ArrayList<StoreImpl> getStores() {
		return null;
	}

	public static StoreImpl addStore() {
		return null;
	}

	public static ArrayList<StoreOwnerImpl> getStoreOwners() {
		return null;
	}

	public static void assignStoreOwner() {
	}

	public static ArrayList<CustomerImpl> getMallVistors() {
		return null;
	}

	public static void logoffVistor() {
	}

	public static void acceptNewVisitorReg() {
	}
	static ProposalImpl proposal;
    static StoreImpl store;
    static Authenticator<Visitor> loginauthenticator= new Authenticator<Visitor>();
	static// Related Objects that interact with the System
	Visitor newsystemuser;
	// Properties of the UI
	private static Scanner scanner = new Scanner(System.in);
	private static String userchoice;
	private static String userinput;
	private static String rpassword;
	private static String password;
	// Constants that represent the choices visitors can make
	private static final String NEW_USER_CHOICE = "N";
	private static final String RETRUNING_USER_CHOICE = "R";
	private static final String NEW_CUSTOMER_CHOICE = "C";
	private static final String NEW_STOREOWNER_CHOICE = "S";
	private static final String RETRUNING_CUSTOMER_CHOICE = "C";
	private static final String RETRUNING_STOREOWNER_CHOICE = "R";

	public static void displayMallFront() {
		System.out.println("For New users Enter N");
		System.out.println("For Returning users Enter R");
		userchoice = scanner.nextLine();
		userchoice = userchoice.trim();

		if (userchoice.equalsIgnoreCase(NEW_USER_CHOICE)) {
			displayNewUserRegistration();
		}
		if (userchoice.equalsIgnoreCase(RETRUNING_USER_CHOICE)) {
			DisplayReturningUserLogin();
		}
	}

	private static void displayNewUserRegistration() {
		System.out.println("Enter C For New Customer Registration");
		System.out.println("Enter S For New StoreOwner Registration");
		userchoice = scanner.nextLine();
		userchoice = userchoice.trim();
		if (userchoice.equalsIgnoreCase(NEW_CUSTOMER_CHOICE)) {
			newsystemuser = new CustomerImpl();
			displaybasicRegistrationForm(newsystemuser);
		}
		if (userchoice.equalsIgnoreCase(NEW_STOREOWNER_CHOICE)) {
			newsystemuser = new StoreOwnerImpl();
			proposal = new ProposalImpl();
			displayStoreOwnerRegistrationForm(newsystemuser, proposal);
		}

	}

	private static void displayStoreOwnerRegistrationForm(Visitor systemuser,ProposalImpl proposal) {
		
		System.out.println("Enter Number of Store Needed");
		proposal.setNumberofstores(Integer.parseInt(getUserinput()));
		System.out.println("Enter Social Security Number");
		proposal.setSsn(Integer.parseInt(getUserinput()));
		System.out.println("Enter Type of Goods to Sell");
		System.out
				.println("Enter P for perishable Goods and N for Non Perishable");
		String itemchoice = getUserinput();
		if (itemchoice.equalsIgnoreCase("N")) {
			proposal.setTypeofitemstosell(new StringWrapper("N"));
		}
		if (itemchoice.equalsIgnoreCase("P")) {
			proposal.setTypeofitemstosell(new StringWrapper("P"));
		}
		displaybasicRegistrationForm(systemuser);
	}

	private static void displaybasicRegistrationForm(Visitor systemuser) {
		systemuser.signup();
		System.out.println("Enter Name");
		systemuser.setName(new StringWrapper(getUserinput()));
		System.out.println("Enter UserName");
		systemuser.setUserName(new StringWrapper(getUserinput()));
		getPassword(systemuser);
		System.out.println("Enter Address");
		systemuser.setAddress(new StringWrapper(getUserinput()));
		System.out.println("Enter City");
		systemuser.setCityName(new StringWrapper(getUserinput()));
		System.out.println("Enter Phone Number");
		systemuser.setPhoneNumber(new StringWrapper(getUserinput()));
		System.out.println("Enter S to submit");
		if(getUserinput().equalsIgnoreCase("S")){
			loginauthenticator.registerCustomer(systemuser);
			}
	}
	private static void getPassword(Visitor systemuser){
		do{
		System.out.println("Enter Password");
		password =getUserinput();
		System.out.println("Enter Reenter Password");
		rpassword =getUserinput();
		}
		while(!password.equals(rpassword));
		systemuser.setPassword(new StringWrapper(password));
			
		
	}

	private static String getUserinput() {
		userinput = scanner.nextLine();
		userinput = userinput.trim();
		return userinput;
	}
private static void DisplayReturningUserLogin(){
	System.out.println("Enter C For Returning Customer login");
	System.out.println("Enter S For Returning StoreOwner login");
	userchoice = scanner.nextLine();
	userchoice = userchoice.trim();
	if (userchoice.equalsIgnoreCase(RETRUNING_CUSTOMER_CHOICE)) {
		newsystemuser = new CustomerImpl();
		displayBasicReturningUserLogin(newsystemuser,null);
	}
	if (userchoice.equalsIgnoreCase(RETRUNING_STOREOWNER_CHOICE)) {
		newsystemuser = new StoreOwnerImpl();
		store = new StoreImpl();
		displayStoreOwnerUserLogin(newsystemuser,store);
	}
	
}
private static void displayStoreOwnerUserLogin(Visitor systemuser, StoreImpl store){
	
	System.out.println("Enter Store ID");
	store.setId(Integer.parseInt(getUserinput()));
	displayBasicReturningUserLogin(systemuser,store);
}
private static void displayBasicReturningUserLogin(Visitor systemuser, StoreImpl store){
	System.out.println("Enter UserName");
	systemuser.setUserName(new StringWrapper(getUserinput()));
	System.out.println("Enter Password");
	String password =getUserinput();
	systemuser.setPassword(new StringWrapper(password));
	System.out.println("Enter S to submit");
	if(getUserinput().equalsIgnoreCase("S")){
		
		System.out.println(loginauthenticator.authenticateVisitor(systemuser));
	}
}
	public static void main(String args[]) {
		displayMallFront();

	}
	

}
